package com.icefish.control;

import com.alibaba.fastjson.JSONObject;
import com.icefish.bean.Order;
import com.icefish.bean.fastjsonParse.Meta;
import com.icefish.dao.OrderDao;
import com.icefish.dao.Shopping_cartDao;
import com.icefish.dao.UserDao;
import com.icefish.daoFactory.DaoFactory;
import com.icefish.utils.Init_Req_Resp_Config;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;

@WebServlet("/simpleBuy")
public class SimpleBuy extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Init_Req_Resp_Config.init(request, response);

        // 1、捞取 购物车数据
           String goods_id = request.getParameter("goods_id");
           String goods_name = request.getParameter("goods_name");
           String goods_picture = request.getParameter("goods_picture");
           String goods_price = request.getParameter("goods_price");
           String shopping_amount = request.getParameter("shopping_amount");
           String shopping_cart_id = request.getParameter("shopping_cart_id");
           String shopping_count = request.getParameter("shopping_count");
           String user_id = request.getParameter("user_id");
           String username = request.getParameter("username");
           String goods_describe = request.getParameter("goods_describe");

        Order order = new Order();
        // 2、uitil.Date 生成订单时间
        order.setOrder_time(new Date());
        // 3、制作 order类

        order.setGoods_id(Integer.parseInt(goods_id));
        order.setGoods_name(goods_name);
        order.setGoods_picture(goods_picture);
        order.setGoods_price(Integer.parseInt(goods_price));
        order.setOrder_amount(Integer.parseInt(shopping_amount));
        order.setOrder_count(Integer.parseInt(shopping_count));
        order.setUser_id(Integer.parseInt(user_id));
        order.setUsername(username);
        order.setGoods_describe(goods_describe);
        order.setSays("买家未做出评价");

        // 4、orderDao执行 insert操作
        Boolean flag = false;

        OrderDao od = DaoFactory.getOrderDaoInstance();
        try {
            flag = od.addOrderBySingleShopCart(order);
        } catch (Exception e) {
            e.printStackTrace();
        }

        Meta meta = new Meta();

        // 5.1、成功添加订单记录。 进一步操作shoping_cartDao 通过id号删除记录
        if (flag) {

            Shopping_cartDao scd = DaoFactory.getShopping_cartDaoInstance();
            UserDao ud = DaoFactory.getUserDaoInstance();
            boolean deleteFlag = false;
            boolean userFlag = false;
            try {
                // 根据购物车id删除购物车记录
                deleteFlag = scd.deleteById(Integer.parseInt(shopping_cart_id));
                //根据用户id获取用户余额
                System.out.println(user_id);
                Integer have_money =  ud.selectWalletByUserId(Integer.parseInt(user_id));
                // 现有金额扣除商品总价
                Integer wallet = have_money - Integer.parseInt(shopping_amount);
                //根据用户ID和订单总金额修改用户钱包余额
                userFlag = ud.updateUserWallet(Integer.parseInt(user_id),wallet);


                // 响应数据给浏览器，彻底ok，meta提示200

                    meta.setMsg("购买成功。1、添加订单记录2、删除购物车记录3、扣除用户金额");
                    meta.setStatus(200);

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        // 5.2、添加订单记录失败，meta提示500
        else {
            meta.setMsg("购买失败");
            meta.setStatus(500);
        }

        JSONObject jsonObject = new JSONObject();
        // 转换成json
        String backToBrowser = jsonObject.toJSONString(meta);

        // 响应数据
        response.getWriter().write(backToBrowser);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
